from haystack import indexes
from .models import Course


# class CourseIndex(indexes.SearchIndex, indexes.Indexable):
#     #设置全文搜素的字段  document=True use_template=False 一会写方法实现 （可以指定在哪些列中搜索关键字）
#     text = indexes.CharField(document=True, use_template=False)
#     #走搜索引擎后 展示的字段数据 model_attr 指向mosql模型的哪个字段
#     id=indexes.IntegerField(model_attr="id")
#     name = indexes.CharField(model_attr='name')
#     level= indexes.CharField(model_attr='level')
#     price = indexes.DecimalField(model_attr='price')
#     discount=indexes.DecimalField(model_attr='discount')
#     teacher= indexes.CharField(model_attr='teacher')
#     intro= indexes.CharField(model_attr='intro')
#     detail= indexes.CharField(model_attr='detail')
#     cover= indexes.CharField(model_attr='cover')
#     students = indexes.IntegerField(model_attr='students')
#
#     #方法设置指向哪个模型类
#     def get_model(self):
#         return Course
#     #方法设置搜索的模型类数据
#     def index_queryset(self, using=None):
#         """Used when the entire index for the model is updated."""
#         return self.get_model().objects.filter(isdel=False)
#     #设置text 字段从哪个列中搜索数据
#     def prepare_text(self, obj):
#         #obj 是当前模型类的实例
#         return obj.name + obj.intro + obj.detail
#
#
#
# from haystack import indexes
# from .models import Course

class CourseIndex(indexes.SearchIndex, indexes.Indexable):
    # 设置全文搜索的字段 document=True use_template=False 一会写方法实现 （可以指定在哪些列中搜索关键字）
    text = indexes.CharField(document=True, use_template=False)
    # 走搜索引擎后 展示的字段数据 model_attr 指向mysql模型的哪个字段
    id = indexes.IntegerField(model_attr="id")
    name = indexes.CharField(model_attr='name')
    level = indexes.CharField(model_attr='level')
    price = indexes.DecimalField(model_attr='price')
    discount = indexes.DecimalField(model_attr='discount')
    teacher = indexes.CharField(model_attr='teacher')
    intro = indexes.CharField(model_attr='intro')
    detail = indexes.CharField(model_attr='detail')
    cover = indexes.CharField(model_attr='cover')
    students = indexes.IntegerField(model_attr='students')

    # 方法设置指向哪个模型类
    def get_model(self):
        return Course

    # 方法设置搜索的模型类数据
    def index_queryset(self, using=None):
        """Used when the entire index for the model is updated."""
        return self.get_model().objects.filter(isdel=False)

    # 设置text 字段从哪个列中搜索数据
    def prepare_text(self, obj):
        # obj 是当前模型类的实例
        return obj.name + " " + obj.intro + " " + obj.detail
